Skip to content

CG-Assignment-2

Question-1

Give a sequence of 4x4 matrices that transforms the parallelogram in the left figure to the unit square in the right. Write down the name of the transformation for each matrix. Also write down the transformation matrix of each step.

mvWXNr

Firstly,Translate the parallelogram to the origin. The translation matrix is:

MTranslation=[1000.6010100100001]

Then sheared the parallelogram to a rectangle. The shear matrix is:

MSheer=[10.200010000100001]

Finally, scale the rectangle to a unit square. The scale matrix is:

MScale=[2.500000.50000100001]

The final transformation matrix is the product of the three matrices:

MComposite=MScaleMSheerMTranslation

Question-2

The following converts an oblique frustum into an orthogonal one where the z-axis coincides with the frustum’s central line. Note that xa=xrxl2 and ya=ytyb2. Give the matrix of the transformation.

hbETjV

To transform the oblique frustum into an orthogonal frustum, we use a shear transformation matrix that aligns the centerline with the z-axis.

  • Original frustum coordinates:
    • Right-top: (xr,yt,zd),
    • Left-bottom: (xl,yb,zd).
  • New orthogonal frustum:
    • Right-top: (xa,ya,zd),
    • Left-bottom: (xa,ya,zd), where xa=xrxl2 and ya=ytyb2.

Shear Factors: To remove the offsets caused by the oblique frustum:

sx=xr+xl2zd,sy=yt+yb2zd.

Shear Transformation Matrix:

Mshear=[10xr+xl2zd001yt+yb2zd000100001].

Question-3

Consider the figure below. i is the direction of the incident light, n is the normal of the surface at S and r is the direction of the reflection. All of them are unit vectors. Derive a formula for r in terms of i and n.

9wWWdS

Decompose the incident vector i:

i=i+i,

where:

  • i=(in)n (component parallel to the normal),
  • i=ii (component perpendicular to the normal).

When reflected:

  • The parallel component i is reversed: i,
  • The perpendicular component i stays the same.

So the reflected vector becomes:

r=ii.

Substitute i:

r=(i(in)n)(in)n.

Simplify:

r=i2(in)n.

Question-4

(a)

Derive the 4×4 homogenous matrix representation of a rotation transformation in 3D space of 30 degree about the directed line through the origin with direction vector (0,1,1).

To derive the homogeneous 4×4 rotation matrix for a rotation of 30 about the direction vector (0,1,1), using Rodrigues' formula:

R(n,α)=cos(α)I+(1cos(α))nnT+sin(α)N.

Normalize the Direction Vector

n=(0,1,1)n=(0,12,12).

Compute N (Skew-Symmetric Matrix)

N=[0nznynz0nxnynx0]=[0121212001200].

Outer Product nnT

nnT=[01212][01212]=[0000121201212].

Plug into Rodrigues' Formula Substitute into the formula for rotation, with α=30:

  • cos(30)=32,
  • sin(30)=12,
  • 1cos(30)=132=232.
R(n,30)=cos(α)I+(1cos(α))nnT+sin(α)N.

Compute Each Term

  1. cos(α)I:
cos(30)I=32[100010001].
  1. (1cos(α))nnT:
(1cos(30))nnT=232[0000121201212].
  1. sin(α)N:
sin(30)N=12[0121212001200].

Add Terms Together Sum the above three terms to get:

R(n,30)=[3212212212232+23423412223432+234].

Create the 4×4 Homogeneous Matrix

Embed the 3×3 rotation matrix into a 4×4 matrix by adding a bottom row and column for homogeneity:

R(n,30)=[32122122012232+234234012223432+23400001].

(b)

What is the image of point (0,3,3) under the transformation in (a)?

v=[0331]v=R(n,30)v

The image of the point (0,3,3) under the given transformation (including homogeneous coordinates) is:

v=[0331]

Question-5

(a)

Given a triangle ABC in 2D plane, show that any point P in the plane can be represented uniquely as show that

P=αA+βB+γC, with α+β+λ=1.
  1. Affine Combination: Any point P in the plane of A,B,C can be written as:P=αA+βB+γC,because A,B,C define the plane (not collinear). To ensure P lies in the affine subspace of the triangle:α+β+γ=1.

  1. Coordinate Representation: Let A=(xA,yA), B=(xB,yB), C=(xC,yC), and P=(xP,yP). Write:xP=αxA+βxB+γxC,yP=αyA+βyB+γyC,with α+β+γ=1. This forms a system of linear equations to uniquely determine α,β,γ.

  1. Uniqueness: Assume P has two representations:P=α1A+β1B+γ1C,α1+β1+γ1=1,P=α2A+β2B+γ2C,α2+β2+γ2=1.Subtract:0=(α1α2)A+(β1β2)B+(γ1γ2)C.Since A,B,C are not collinear (linearly independent), the only solution is:α1=α2,β1=β2,γ1=γ2.

Therefore, any point P in the plane can be written uniquely as:

P=αA+βB+γC,with α+β+γ=1.

(b)

P is inside ABC or on one of its sides if and only if α, β, λ are all nonnegative.

To show that P is inside ABC or on its sides if and only if α,β,γ0:

  1. Sufficient Condition (α,β,γ0): If α,β,γ0 and α+β+γ=1, P=αA+βB+γC is a convex combination.

    • If α,β,γ>0, P is inside the triangle.
    • If one of α,β,γ=0, P is on an edge of ABC.
    • If two of α,β,γ=0, P is at a vertex.
  2. Necessary Condition: If P is inside ABC or on its sides, P can be written as P=αA+βB+γC where α+β+γ=1. In these cases:

    • P inside implies α,β,γ>0.
    • P on edges or vertices implies α,β,γ0.

Thus, P is inside or on ABC if and only if α,β,γ0 and α+β+γ=1.


(c)

Given a triangle ABC in 2D plane with

A=(11),B=(43),andC=(76),

determine if the point

P=(54)

is inside ABC by computing the barycentric coordinates of P with respect to ABC.

To determine if P=(54) is inside ABC, compute its barycentric coordinates. The vertices of the triangle are:

A=(11),B=(43),C=(76).

Barycentric coordinate equations

Point P satisfies:

P=αA+βB+γC,whereα+β+γ=1.

This expands into:

5=α1+β4+γ7,4=α1+β3+γ6.

From α+β+γ=1, we have α=1βγ. Substitute into the other equations:

5=(1βγ)+4β+7γ,4=(1βγ)+3β+6γ.

Simplify both equations:

5=1+3β+6γ,4=1+2β+5γ.

Reduce:

4=3β+6γ,3=2β+5γ.

Solve the system by elimination: Subtract 2β+5γ=3 from 3β+6γ=4:

β+γ=1.

Substitute γ=1β into 2β+5γ=3:

2β+5(1β)=3,2β+55β=3,3β=2,β=23.

Thus:

γ=1β=13,α=1βγ=0.

The barycentric coordinates of P are:

α=0,β=23,γ=13.

Since α,β,γ0 and α+β+γ=1, point P lies on the edge of ABC. Specifically, P is on the line segment BC.

Question-6

The following code is written to draw the outer surface of a cylinder as a quad-strip but nothing is seen on the screen. Assuming that lighting parameters and reflectance are properly set, explain why and suggest a remedy.

nVpzgm

The issue is caused by incorrect vertex winding: the vertices are specified in clockwise order, making the quads back-facing by default. Since glCullFace(GL_BACK) is enabled, these polygons are culled, leaving nothing visible.

Remedy: Change the vertex order to counterclockwise for proper front-facing polygons:

cpp
glPolygonMode(GL_FRONT, GL_FILL);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);

int nslice = 20;
glBegin(GL_QUAD_STRIP);
double t = 0., dt = 2. * 3.1416 / nslice;
for (int j = 0; j <= nslice; ++j) {
    glNormal3f(cos(t), 0., -sin(t));
    glVertex3f(cos(t), 2., -sin(t)); // Top vertex first
    glVertex3f(cos(t), 0., -sin(t)); // Bottom vertex second
    t += dt;
}
glEnd();

Question-7

Consider the function sphere() that draws the outer surface of a unit sphere. Assume that a cylindrical map of the earth has been set up as the current texture and lighting is enabled. Suggest modifications on the function for supporting lighting calculation and the mapping of the texture on the sphere.

3VnqTf

To modify the sphere() function for lighting and texture mapping:

  1. Add Normals for Lighting: Use the vertex position as normals:

    c
    glNormal3f(r1 * ct, y1, r1 * st); // For top vertex
    glNormal3f(r2 * ct, y2, r2 * st); // For bottom vertex
  2. Implement Texture Mapping: Calculate texture coordinates using spherical coordinates:

    c
    glTexCoord2f(theta / (2.0 * M_PI), alpha1 / M_PI); // For top vertex
    glTexCoord2f(theta / (2.0 * M_PI), alpha2 / M_PI); // For bottom vertex

Updated sphere() Function:

cpp
void sphere() {
    double alpha1, alpha2, da, theta, dtheta, y1, y2, r1, r2, ct, st;
    int nslice = 50, nstack = 20;

    da = M_PI / nstack;
    dtheta = 2.0 * M_PI / nslice;

    alpha1 = 0.0; y1 = cos(alpha1); r1 = sin(alpha1);

    for (int i = 1; i <= nstack; ++i) {
        alpha2 = alpha1 + da;
        y2 = cos(alpha2); r2 = sin(alpha2);

        glBegin(GL_QUAD_STRIP);
        theta = 0.0;

        for (int j = 0; j <= nslice; ++j) {
            ct = cos(theta); st = sin(theta);

            glTexCoord2f(theta / (2.0 * M_PI), alpha1 / M_PI);
            glNormal3f(r1 * ct, y1, r1 * st); // Normal for top
            glVertex3f(r1 * ct, y1, r1 * st);

            glTexCoord2f(theta / (2.0 * M_PI), alpha2 / M_PI);
            glNormal3f(r2 * ct, y2, r2 * st); // Normal for bottom
            glVertex3f(r2 * ct, y2, r2 * st);

            theta += dtheta;
        }
        glEnd();

        alpha1 = alpha2; y1 = y2; r1 = r2;
    }
}